package com.road7.mina.customCodec;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.transport.socket.SocketSessionConfig;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

/**
 * @Description :
 * @FileName: Server1.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年11月20日 上午10:43:44
 * @Version:V1.00
 */
public class Server3 {

	public static void main(String[] args) throws IOException {
		IoAcceptor clientAcceptor = new NioSocketAcceptor(Runtime.getRuntime().availableProcessors() + 1);
		SocketSessionConfig config = (SocketSessionConfig) clientAcceptor.getSessionConfig();
		config.setReceiveBufferSize(1024 * 64);
		config.setSendBufferSize(1024 * 4);
		config.setSoLinger(0);// 设置了它后，MINA在调用了close()方法后，不会再进入TIME_WAIT状态了，而直接Close掉了，这样就不会产生这样的那些TIME_WAIT的状态了。
		config.setReuseAddress(false);
		config.setTcpNoDelay(true);
		config.setIdleTime(IdleStatus.BOTH_IDLE, 60 * 2);

		clientAcceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ProtobufStrictCodecFactory()));
		Executor threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 4 + 1);
		clientAcceptor.getFilterChain().addLast("execute", new ExecutorFilter(threadPool));
		clientAcceptor.setHandler(new ClientRouteHandler());
		clientAcceptor.bind(new InetSocketAddress(ServerPort.PORT3));
	}
}
